package com.wmcsk;
import android.app.Activity;
import android.content.Context;
import android.view.ViewGroup;
import com.wmcsk.defaultImp.DefaultChapingAdapter;
import com.wmcsk.dl.util.LogUtils;
import com.wmcsk.interfaces.GuanggaoListener;
import com.wmcsk.interfaces.InitResultListener;
import com.wmcsk.interfaces.KYSdkInterface;
import com.wmcsk.interfaces.SplashAdListener;

/**
 * Created by wm on 2017/7/6.
 */
public class KuaiyanSdk implements KYSdkInterface{
    private static KuaiyanSdk kuaiyanSdk;
    public static final String ERROR_TAG = "KuaiyanSdk";
    private KuaiyanSdk(){

    }
    public static KuaiyanSdk getInstance(){
        if(kuaiyanSdk == null){
            synchronized (KuaiyanSdk.class){
                if(kuaiyanSdk == null){
                    kuaiyanSdk = new KuaiyanSdk();
                }
            }
        }
        return kuaiyanSdk;
    }

    private KYSdkInterface kySdkInterface;
    public void initKYSdkInterface(KYSdkInterface kySdkInterface){
        this.kySdkInterface = kySdkInterface;
    }
    /**
     * 获取插件版本号
     * @return
     */
   public String getVersion(){
       if(kySdkInterface == null){
           LogUtils.getNewInstance(ERROR_TAG).eob("please wait sdkinit init complate");
           return "null";
       }
       return kySdkInterface.getVersion();
   }
    /**
     * 请求广告
     */
    public void requestAd(String onlySimple, ViewGroup view, String id, GuanggaoListener gg){
        if(kySdkInterface == null) {
            LogUtils.getNewInstance(ERROR_TAG).eob("please wait sdkinit init complate");
            if(gg!=null){
                gg.threadError(-10,"初始化未完成",new RuntimeException("初始化未完成"));
            }
            return;
        }
        kySdkInterface.requestAd(onlySimple,view,id,gg);
    }

    public void requestChapingAd(final String onlySimple,ViewGroup view,String id,  DefaultChapingAdapter gg){
        if(kySdkInterface == null) {
            LogUtils.getNewInstance(ERROR_TAG).eob("please wait sdkinit init complate");
            if(gg!=null){
                gg.threadError(-10,"初始化未完成",new RuntimeException("初始化未完成"));
            }
            return;
        }
        kySdkInterface.requestChapingAd(onlySimple,view,id,gg);
    }
    /**
     * 初始化插件
     * @param context
     * @param initResultListener
     */
    public void init(Context context, InitResultListener initResultListener){
        if(kySdkInterface == null) {
            LogUtils.getNewInstance(ERROR_TAG).eob("please wait sdkinit init complate");
            if(initResultListener!=null){
                initResultListener.initResult(-10);
            }
            return;
        }
        kySdkInterface.init(context,initResultListener);
    }

    @Override
    public String getSdkInfo() {
        if(kySdkInterface == null) {
            LogUtils.getNewInstance(ERROR_TAG).eob("please wait sdkinit init complate");
            return null;
        }
        return kySdkInterface.getSdkInfo();
    }

    @Override
    public void setAppKye(String appKey) {
        if(kySdkInterface == null){
            LogUtils.getNewInstance(ERROR_TAG).eob("please wait sdkinit init complate");
            return;
        }
        kySdkInterface.setAppKye(appKey);
    }

    @Override
    public void requestSplashAd(Activity activity, ViewGroup viewGroup, SplashAdListener splashAdListener, String id, boolean canclick) {
        if(kySdkInterface == null){
            LogUtils.getNewInstance(ERROR_TAG).eob("please wait sdkinit init complate");
            if(splashAdListener!=null){
                splashAdListener.onAdFailed("初始化未完成");
            }
            return;
        }
        kySdkInterface.requestSplashAd(activity,viewGroup,splashAdListener,id,canclick);
    }
}
